import Vue from "vue";
import Vuex, { Store } from "vuex";

Vue.use(Vuex);

const state = {
    todos:[
    ]
}
const getters = {
    total(state){
        return state.todos.length;
    },
    isAllChecked(state,getters){
        return  getters.total > 0  && getters.total === getters.doneTotal
    }
}

const actions = {}

const mutations = {
    addtodo(state,todoObj){
        state.todos.unshift(todoObj);
    },
    cheaktodo(state,id){
        state.todos.forEach(todo => {
            if(todo.id === id) todo.done = !todo.done;
        })
    },
    deltodo(state,id){
        state.todos = state.todos.filter(todo => todo.id !== id);
    },
}

export default new Store({
    state,
    getters,
    actions,
    mutations
})